package com.servlet.book;

import com.enitiy.Book;
import com.service.book.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/28 14:43
 * 项目名称 SHUSHAN
 */
@WebServlet("/BookAdd")
@MultipartConfig
public class BookAdd extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("表单上传图片");
        //测试获取表单控件的数据
        req.setCharacterEncoding("utf-8");//服务端接收前台数据防止中文乱码
        Part part = req.getPart("img");
        String file=null;
        if (part==null){
             file="1.jpg";
        }
        String book = req.getParameter("book");
        String user = req.getParameter("user");
        int kid = Integer.parseInt(req.getParameter("kid"));
        String synopsis = req.getParameter("synopsis");
        String url = req.getParameter("url");
         file = part.getSubmittedFileName();
        String path = "C:\\Users\\MySunshine\\Desktop\\SHUSHAN IS DETERMINED\\SHUSHAN\\web\\userimg\\"+file;
        part.write(path);
        BookServiceImpl bsi = new BookServiceImpl();
        boolean b = bsi.AddBook(new Book(book, user, kid, synopsis, file, url));
        if (b){
            System.out.println("添加成功！");
            resp.sendRedirect("/page/dba/dbaBookMge.jsp");
        }else {
            System.out.println("添加失败！");
            resp.sendRedirect("/page/book/bookAdd.jsp");
        }
    }
}
